package org.apache.jmeter.functions;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import com.aliyun.openservices.shade.org.apache.commons.codec.digest.DigestUtils;



public class GetOpenapiSignJmeter extends AbstractFunction{
	
	//添加方法描述
    private static final List<String> desc = new LinkedList<String>();
    //初始化代码块定义函数名称
    static {
        desc.add("appKey");
        desc.add("curtime");
        desc.add("nonce");
        desc.add("appSecret");
    }
    //function名称
    private static final String KEY = "__GetOpenapiAppsign";
    private static final int num = 4;
    
    //传入参数的值
    private Object[] values;

    
    /**功能描述**/
    public List<String> getArgumentDesc() {
    	
        return desc;
    }
	
    
    @Override
    public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    	
    	String appKey=((CompoundVariable)values[0]).execute().trim();
    	String curtime=((CompoundVariable)values[1]).execute().trim();
    	String nonce=((CompoundVariable)values[2]).execute().trim();
    	String appSecret=((CompoundVariable)values[3]).execute().trim();
    	
        try {
            Thread.sleep(1000);
            String str="";
    		String strs=str+appSecret;
    		return DigestUtils.sha1Hex(strs);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            throw new InvalidVariableException(e);
        }
    }

    /**
     * 设置参数。接收用户传递的参数
     * @param collection
     * @throws InvalidVariableException
     * **/
    @Override
    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
    	//检查参数数量是否设置正确
        checkParameterCount(parameters, num);
        values = parameters.toArray();
        //获取具体参数值       
    }

    /**返回function名称**/
    @Override
    public String getReferenceKey() {
        return KEY;
    }

	
	

}
